/*package com.znf4.controller.search.lucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoubleField;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.springframework.stereotype.Component;
import com.platform.common.contants.Constants;
import com.platform.entity.vo.Good_Type1_2_User;
import com.znf4.good.free.entity.Free;


*//**
 * 索引管理Dao实现类
 *//*
@Component
public class IndexDao  {

	*//**
	 * 创建索引库
	 *//*
	public void createDump(List<Free> list ) {
		
		//获得IndexWriter对象
		IndexWriter writer = IndexManager.getIndexWriter();
		//创建索引前先清空
		try {
			writer.deleteAll();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//重新建立索引
		for (Good_Type1_2_User model:list) {
			
			
			//创建文档对象
			Document document = new Document();
			
			Field id = new IntField("pid", model.getPid(), Store.YES);
			Field name = new TextField("name", model.getName(), Store.YES);
			Field catalog = new IntField("catalog", model.getCatalog(), Store.YES);
			Field catalog_name = new StringField("catalog_name", model.getCatalog_name(), Store.YES);
			Field price = new DoubleField("price", model.getPrice(), Store.YES);
			Field number = new IntField("number", model.getNumber(), Store.YES);
			
			Field description = new TextField("discription", model.getDescription(), Store.NO);
			
			Field releaseTime = new LongField("release_time", model.getRelease_time().getTime(), Store.YES);
			
			
			Field picture = new StoredField("picture", model.getPicture());
			
			
			//创建索引域
			*//**商品  表**//*
			Field  good_id              = new StoredField("good_id", model.getGood_id()) ;      // 不分词，不索引，存储
			Field  good_name            = new TextField("good_name", model.getGood_name(), Store.YES) ;
			Field  good_price           = new DoubleField("good_price", Double.parseDouble( model.getGood_price() ), Store.YES) ;
			Field  good_msg             = new TextField("good_msg", model.getGood_msg(), Store.NO) ;  
			Field  good_how_old         = new IntField("good_how_old", Integer.parseInt( model.getGood_how_old() ),Store.YES ) ;
			Field  good_img             = new StoredField("good_img", model.getGood_img()) ;
			Field  good_extension_state = new StoredField("good_extension_state", model.getGood_extension_state()) ;
			//Field  good_great_time  = new LongField("good_great_time", model.getGood_great_time().getTime(), Store.YES) ;
			Field  good_tj_state    = new IntField("good_tj_state", model.getGood_tj_state(), Store.YES) ;    // 推荐状态
			
			*//**类别1**//*
			Field  good_type1_id = new IntField("good_type1_id", model.getGood_type1_id(), Store.YES) ;           // 类别1 的ID
			Field  good_type1_name = new StringField("good_type1_name", model.getGood_type1_name(), Store.YES) ;      // 类别1 的类名
			
			*//**类别2**//*
			Field  good_type2_id = new IntField("good_type2_id", model.getGood_type2_id(), Store.YES) ;
			Field  good_type2_name = new StringField("good_type2_name", model.getGood_type2_name(), Store.YES) ;      // 类别2 的类名
			
			
			*//***点击量***//*
			Field  click_num = new IntField("click_num", model.getClick_num(), Store.YES);  ;          //  商品的点击量 
			
			if(model.getGood_extension_state() == 1){   // 推广 状态
				System.out.println("推广的值 ：" + model.getGood_extension_number());
				good_name.setBoost(model.getGood_extension_number());   // 添加推广
			}
			
			
			//将域添加到文档对象中
			document.add(good_id);
			document.add(good_name);
			document.add(good_price);
			document.add(good_msg);
			document.add(good_how_old);
			document.add(good_img);
			//document.add(good_great_time);    创建时间
			document.add(good_tj_state);
			//document.add(good_type1_id);      类别一的ID
			//document.add(good_type1_name);    类别一的名字
			document.add(good_type2_id);
			document.add(good_type2_name);
			//document.add(username);
			document.add(click_num);
			document.add(good_extension_state);
			
			try {
				//写索引库
				writer.addDocument(document);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			//提交
			writer.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	
	
	*//**
	 * 更加分类名称查询商品分类列表
	 * catalog_name 二级类别名字
	 * sort 1 : 价格  ，2 :新旧 , 3 : 推荐 , 4 : 点击量
	 *//*
	public GoodResult getProductList(String good_name,String good_type2_name, Double priceStart,Double priceEnd, Integer page, String sort) {
		
		// 根据分类名称查询索引
		//查询的字段
		BooleanQuery query = new BooleanQuery();
		Query keyWordQuery = null;
		if (good_name != null && !"".equals(good_name)) {
			//查询域列表
			String [] fields = {"good_name","good_msg"};
			//创建查询分析器
			QueryParser queryParser = new MultiFieldQueryParser(fields, IndexManager.getAnalyzer());
			//根据搜索关键词创建查询
			try {
				keyWordQuery = queryParser.parse(good_name);
				query.add(keyWordQuery, Occur.MUST);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else {
			MatchAllDocsQuery matchAllDocsQuery = new MatchAllDocsQuery();
			query.add(matchAllDocsQuery, Occur.MUST);
		}
		//根据商品分类名称查询
		if (good_type2_name != null && !"".equals(good_type2_name)) {
			Query catalogQuery = new TermQuery(new Term("good_type2_name",good_type2_name));
			query.add(catalogQuery, Occur.MUST);
		}
		
		//根据价格区间过滤
		Filter filter = null;
		if (priceStart != null) {
			filter = NumericRangeFilter.newDoubleRange("good_price", priceStart , priceEnd, true, true);
		}
		//排序，默认根据价格排序
		Sort sSort = new Sort();
		if ("1".equals(sort)) { 
			SortField sortField = new SortField("good_price", SortField.Type.DOUBLE);
			sSort.setSort(sortField);
		} else if("2".equals(sort)){
			SortField sortField = new SortField("good_how_old", SortField.Type.INT, true);
			sSort.setSort(sortField);
		} else if("3".equals(sort)){	  
			SortField sortField = new SortField("good_tj_state", SortField.Type.INT, true);
			sSort.setSort(sortField);
		} else if("4".equals(sort)){
			SortField sortField = new SortField("click_num", SortField.Type.INT, true);
			sSort.setSort(sortField);
		} 
		
		GoodResult resultList =queryIndex(query, filter, sSort, page);
		//如果是根据关键字查询的结果，需要高亮显示关键字
		if (keyWordQuery != null) {
			QueryScorer queryScorer = new QueryScorer(keyWordQuery);
			SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style=\"color:red\">", "</span>");
			Highlighter highlighter = new Highlighter(formatter, queryScorer);
			if (null != resultList.getProductList()) {
				for (Good_Type1_2_User model:resultList.getProductList()) {
					try {
						//高亮显示商品名称中的关键字
						String name = highlighter.getBestFragment(IndexManager.getAnalyzer(), "good_name", model.getGood_name());
						System.out.println(model.getGood_name() + "=>" + name);
						//如果商品名称中没有关键字则返回null，此时不更新。
						if (null != name) {
							model.setGood_name(name);
						}
					} catch (Exception e) {
						e.printStackTrace();
					} 
					
				}
			}
		}
		return resultList;
	}
	
	
	*//**
	 * 通用查询方法，根据指定的Query对象查询索引库
	 *//*
	private GoodResult queryIndex(Query query, Filter filter, Sort sort, int page) {
		//创建结果对象
		GoodResult resultModel = new GoodResult();
		
		//获得IndexSearcher对象
		IndexSearcher searcher = IndexManager.getIndexSearcher();
		//商品列表
		List<Good_Type1_2_User> productList = new ArrayList<>();
		try {
			ScoreDoc[] scoreDocs = searcher.search(query, filter, Constants.Max , sort).scoreDocs;
			//商品总数
			resultModel.setRecordCount(scoreDocs.length);
			//计算总页数
			int pageCount = scoreDocs.length / Constants.PageSize ;
			if ((scoreDocs.length % Constants.PageSize) > 0) {
				pageCount ++;
			}
			resultModel.setPageCount(pageCount);
			//当前页
			resultModel.setCurPage(page);
			if (page > pageCount) {
				return resultModel;
			}
			//计算起始和结束下标
			int start = (page - 1) * Constants.PageSize ;
			int end = page * Constants.PageSize ;
			
			//起始下标大于记录总数
			if (start < 0 || start > scoreDocs.length) {
				return resultModel;
			}
			//结束下标大于记录总数时
			if (end > scoreDocs.length) {
				end = scoreDocs.length;
			}
			
			//遍历查询索引结果
			for (int i = start; i < end; i++) {
				Document document = searcher.doc(scoreDocs[i].doc);
				//商品对象
				Good_Type1_2_User model = new Good_Type1_2_User();
				model.setGood_id( document.get("good_id" )) ;  
				model.setGood_name(document.get("good_name") ) ;
				model.setGood_price(document.get("good_price") ) ;
				model.setGood_img( document.get("good_img") ) ;
				model.setGood_extension_state(Integer.parseInt(  document.get("good_extension_state")  ));
				//将商品对象添加到列表中
				productList.add(model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultModel.setProductList(productList);
		return resultModel;
	}
	
	
	
	*//**
	 * 实时索引
	 *//*
	public boolean  addIndexDocument(List<Good_Type1_2_User>  list){
		
		IndexUtils indexHelp = IndexUtils.getIndexHelp() ;
		
		boolean  flag = false ;
		//重新建立索引
		for (Good_Type1_2_User model:list) {
			
			//创建文档对象
			Document document = new Document();
			
			//创建索引域
			*//**商品  表**//*
			Field  good_id          = new StoredField("good_id", model.getGood_id()) ;      // 不分词，不索引，存储
			Field  good_name        = new TextField("good_name", model.getGood_name(), Store.YES) ;
			Field  good_price       = new DoubleField("good_price", Double.parseDouble( model.getGood_price() ), Store.YES) ;
			Field  good_msg         = new TextField("good_msg", model.getGood_msg(), Store.NO) ;  
			Field  good_how_old     = new IntField("good_how_old", Integer.parseInt( model.getGood_how_old() ),Store.YES ) ;
			Field  good_img         = new StoredField("good_img", model.getGood_img()) ;
			Field  good_tj_state    = new IntField("good_tj_state", model.getGood_tj_state(), Store.YES) ;    // 推荐状态
			Field  good_extension_state = new StoredField("good_extension_state", model.getGood_extension_state()) ;

			*//**类别2**//*
			Field  good_type2_id = new IntField("good_type2_id", model.getGood_type2_id(), Store.YES) ;
			Field  good_type2_name = new StringField("good_type2_name", model.getGood_type2_name(), Store.YES) ;      // 类别2 的类名
			
			*//***点击量***//*
			Field  click_num = new IntField("click_num", model.getClick_num(), Store.YES);  ;          //  商品的点击量 
			
			
			if(model.getGood_extension_state() == 1){   // 推广 状态
				System.out.println("推广的值 ：" + model.getGood_extension_number());
				good_name.setBoost(model.getGood_extension_number());   // 添加推广
			}
		
			//将域添加到文档对象中
			document.add(good_id);
			document.add(good_name);
			document.add(good_price);
			document.add(good_msg);
			document.add(good_how_old);
			document.add(good_img);
			document.add(good_tj_state);
			document.add(good_type2_id);
			document.add(good_type2_name);
			document.add(click_num);
			document.add(good_extension_state);
			
			try {
				//写索引库
				flag = indexHelp.addDocument(document) ;
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return flag ;
	}
	
	
	
	
	
	
	
	
	
	
	

}
*/